package top.meeting.room.module.system.model;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import top.meeting.room.baseModel.BaseRole;

import java.util.LinkedList;
import java.util.List;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/30
 * FOR : RoleBean
 */
public class Role extends BaseRole<Role> {
    public static final Role dao = new Role().dao();


    /**
     * SqlParas And Caches
     */
    public static final String role_getModel = "role.getModel";
    public static final String role_pageModel = "role.pageModel";
    public static final String role_getRecord = "role.getRecord";
    public static final String role_getForces = "role.getForces";
    public static final String role_pageRecord = "role.pageRecord";
    public static final String role_getForceIds = "role.getForceIds";
    public static final String role_deleteByIds = "role.deleteByIds";
    public static final String role_findByRoleName = "role.findByRoleName";

    /**
     * 通过
     *
     * @return
     */
    public List<Record> _getForces() {
        List<Record> retVal = CacheKit.get(role_getForces, String.valueOf(this.getForces()));
        if (retVal == null && StrKit.notBlank(this.getForces())) {
            retVal = Force.dao.group(this.getForces());
            CacheKit.put(role_getForces, String.valueOf(this.getForces()), retVal);
        }
        return retVal;
    }

    /**
     * 遍历所有的权限及其子集得到id
     *
     * @return
     */
    public List<Long> getForceIds() {
        List<Long> retVal = CacheKit.get(role_getForceIds, String.valueOf(this.getId()));
        if (retVal == null && StrKit.notNull(this._getForces())) {
            retVal = new LinkedList<Long>();
            for (Record root : this._getForces()) {
                if (root != null) {
                    if (!retVal.contains(root.get(Force.id)) && root.get(Force.id) != null)
                        retVal.add(root.get(Force.id));
                    List<Record> menus = root.get("sonForces");
                    if (StrKit.notNull(menus)) {
                        for (Record menu : menus) {
                            if (menu != null) {
                                if (!retVal.contains(menu.get(Force.id)) && menu.get(Force.id) != null)
                                    retVal.add(menu.get(Force.id));
                                List<Force> buttons = menu.get("sonForces");
                                if (StrKit.notNull(buttons)) {
                                    for (Force button : buttons) {
                                        if (!retVal.contains(button.get(Force.id)) && button.get(Force.id) != null) {
                                            retVal.add(button.get(Force.id));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            CacheKit.put(role_getForceIds, String.valueOf(this.getId()), retVal);
        }
        return retVal;
    }

    /**
     * 重载 toRecord
     *
     * @return
     */
    public Record toBack() {
        Record retVal = super.toRecord();
        return retVal == null ? null :
                retVal.set("forceList", _getForces()).set("forceIds", getForceIds()).remove(forces);
    }
}
